<?php

namespace Wechat\Controller;

use Common\Controller\MyController;
use Common\Service\WechatSdkService;
use Common\Service\MerUserService;

class WechatController extends MyController {

  var $WechatSdk = null;
  var $mer_id    = null;
  var $wx_id     = null;
  var $pid       = 0 ;  
  var $appid     = null;
  var $is_micro_messenger = FALSE ;
  
  const SESSION_NAME = 'ry_wechat_session';
 
  function __construct() {
    parent::__construct();
    
    $this->mer_id = I('get.mer_id', '');
    $this->wx_id  = I('get.wx_id', '');
    $this->pid    = I('get.pid' , 0 );
    
    $user_agent = getenv('HTTP_USER_AGENT');
    if ( strpos($user_agent, 'MicroMessenger') === false ) {
      $this->is_micro_messenger = FALSE ;
    } else {
      $this->is_micro_messenger = TRUE ;
    }
  }

  /**
   * 取用户信息
   * session_array = array {
   *   mer_id : 1   , //商户ID
   *   wx_id  : 25  , //微信ID
   *   uid    : 112 , //用户ID
   *   follow : 1   , //是否关注
   * }
   * @return boolean
   * 
   */
  function _get_user_info() {
    
    if (!$this->WechatSdk) {
      $this->WechatSdk = new WechatSdkService($this->mer_id, $this->wx_id);
    }
    
    $userinfo = session( self::SESSION_NAME );
    //dump( $userinfo );
    if (!empty($userinfo)) {
      //echo $userinfo['mer_id'] . '=' . $this->mer_id ;
      //echo $userinfo['wx_id'] . '=' . $this->wx_id ;
      //if ($userinfo['mer_id'] == $this->mer_id && $userinfo['wx_id'] == $this->wx_id) {
        return $userinfo;
      //}
    }

    $code = I('get.code' , '' );
    

    if ( empty( $code ) ) {
      return FALSE;
    }
    
    //取 user_access_token
    $ret_access_token = $this->WechatSdk->getOauthAccessToken();
    if (!$ret_access_token) {
      return FALSE;
    }

    //取用户信息
    $ret_userinfo = $this->WechatSdk->getUserInfo($ret_access_token['openid']);

    if (!$ret_userinfo) {
      return FALSE;
    }

    //如果没有关注 则通过授权方式取
    if ($ret_userinfo['subscribe'] == 0) {
      $ret_userinfo = $this->WechatSdk->getOauthUserinfo($ret_access_token['access_token'], $ret_access_token['openid']);
      if (!$ret_userinfo) {
        return FALSE;
      }
      $ret_userinfo['subscribe'] = 0;
    }
    
    //添加更新用户
    $MerUser = MerUserService::instance();
    $add_ret = $MerUser->create_by_wechat($this->mer_id, $this->wx_id, $ret_userinfo , $this->pid );
    if ($add_ret['status']) {
      $new_user = $add_ret['data'];
      session(self::SESSION_NAME, $new_user);
      return $new_user;
    } else {
      return FALSE;
    }
  }

  /**
   * 取js config
   * 
   * 返回 array(
   *  "appid" => $this->appid,
   *  "noncestr" => $noncestr,
   *  "timestamp" => $timestamp,
   *  "url" => $url,
   *  "signature" => $sign
   *)
   */
  function _get_js_config() {
    if (!$this->WechatSdk) {
      $this->WechatSdk = new WechatSdkService($this->mer_id, $this->wx_id);
    }

    // 注意 URL 一定要动态获取，不能 hardcode.
    $protocol = (!empty(getenv('HTTPS')) && getenv('HTTPS') !== 'off' || getenv('SERVER_PORT') == 443) ? "https://" : "http://";
    $url = $protocol . getenv('HTTP_HOST') . getenv('REQUEST_URI') ;
      
    $config = $this->WechatSdk->getJsSign($url);

    return $config;
  }

  function _get_cur_url() {
    $protocol = (!empty(getenv('HTTPS')) && getenv('HTTPS') !== 'off' || getenv('SERVER_PORT') == 443) ? "https://" : "http://";
    return $protocol . getenv('HTTP_HOST') . getenv('REQUEST_URI') ;
  }

  /**
   * 上传路径
   * @param type $type
   */
  function upload($type = 'icon') {
    $upload = \Common\Service\UploadService::instance();
    $ret = $upload->do_upload($type);
    $this->ajaxReturn($ret);
  }
  
  /**
   * 删除图标
   * @return type
   */
  function delete_icon() {
    $icon = I('post.icon');
    if ( !strstr($icon, 'Public/upload/icon') ) {
      return ;
    }
    delete_file($icon);
  }
}
